/*
 * Created by JFormDesigner on Mon Jun 24 15:05:33 CST 2024
 */

package scct.yangxianpeng.ui;

import scct.yangxianpeng.dao.UserDao;
import scct.yangxianpeng.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 35403
 */
public class FormLogin extends JFrame {
    FormMain main;

    public static void main(String[] args) {
        FormLogin formLogin = new FormLogin();
        formLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        formLogin.setVisible(true);
    }

    public FormLogin() {
        initComponents();
    }

    private void btnLogin(ActionEvent e) {
        String name = txtName.getText();
        String pass = txtPass.getText();
        System.out.println(name + "," + pass);
//        name.matches("正则表达式")  返回ture表示成功，返回false表示验证失败
        if (!name.matches("^\\w{3,10}$")) {
//            swing的消息对话框
            JOptionPane.showMessageDialog(this, "你输入的用户名不合法，请检查后重新输入！");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")) {
            JOptionPane.showMessageDialog(this, "你输入的密码不合法，请检查后重新输入！");
        }
        UserDao userDao = new UserDao();


        User user = userDao.login(name, pass);
        if (user == null) {
            JOptionPane.showMessageDialog(this, "用户名或密码错误，请见检查后再登录！");
        } else {
            JOptionPane.showMessageDialog(this, "登录成功！");
            FormMain formMain = new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800, 600);
            formMain.setVisible(true);
//            进入主窗体
        }
    }

    private void btnReg(ActionEvent e) {
        DlgUserAdd dlgUserAdd = new DlgUserAdd(main);
        dlgUserAdd.setModal(true);
        dlgUserAdd.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - yang
        labelName = new JLabel();
        labelPass = new JLabel();
        txtName = new JTextField();
        txtPass = new JTextField();
        labelTop = new JLabel();
        btnLogin = new JButton();
        btnReg = new JButton();

        //======== this ========
        setTitle("\u5546\u5e97\u7ba1\u7406\u7cfb\u7edf");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- labelName ----
        labelName.setText("\u7528\u6237\u540d:");
        contentPane.add(labelName);
        labelName.setBounds(new Rectangle(new Point(115, 105), labelName.getPreferredSize()));

        //---- labelPass ----
        labelPass.setText("\u5bc6\u7801:");
        contentPane.add(labelPass);
        labelPass.setBounds(new Rectangle(new Point(125, 155), labelPass.getPreferredSize()));

        //---- txtName ----
        txtName.setText("user");
        contentPane.add(txtName);
        txtName.setBounds(185, 95, 130, txtName.getPreferredSize().height);

        //---- txtPass ----
        txtPass.setText("123");
        contentPane.add(txtPass);
        txtPass.setBounds(185, 145, 130, txtPass.getPreferredSize().height);

        //---- labelTop ----
        labelTop.setText("\u767b\u5f55");
        labelTop.setFont(labelTop.getFont().deriveFont(Font.BOLD, 22f));
        contentPane.add(labelTop);
        labelTop.setBounds(225, 40, 50, 35);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.addActionListener(e -> btnLogin(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(155, 215), btnLogin.getPreferredSize()));

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        btnReg.addActionListener(e -> btnReg(e));
        contentPane.add(btnReg);
        btnReg.setBounds(new Rectangle(new Point(265, 215), btnReg.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(500, 375);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - yang
    private JLabel labelName;
    private JLabel labelPass;
    private JTextField txtName;
    private JTextField txtPass;
    private JLabel labelTop;
    private JButton btnLogin;
    private JButton btnReg;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
